/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elderberryni2.data.container;

import elderberryni2.abstracts.BaseTimestampable;
import elderberryni2.interfaces.IDumpable;

/**
 *
 * @author warringer
 */
public class DataContainer extends BaseTimestampable<Float> implements IDumpable, Comparable<DataContainer> {
    
    private AccelerationContainer acceleration = null;
    private HeadingContainer heading = null;
    private LocationContainer location = null;
    private EnviroContainer enviro = null;
    private RotationContainer rotation = null;
    private VelocityContainer velocity = null;
    
    private float UTC;
    private String body, uuid;

    /**
     * 
     * @param dataString
     */
    public DataContainer(String dataString) {
        String[] data = dataString.split(";");
        
        UTC      = Float.parseFloat(data[23]);
        this.setTimestamp((float) Float.parseFloat(data[24]));
        
        float X     = Float.parseFloat(data[ 0]);  // Roll
        float Y     = Float.parseFloat(data[ 1]);  // Pitch
        float Z     = Float.parseFloat(data[ 2]);  // Yaw
        float V     = 0;
        rotation = new RotationContainer(X, Y, Z, this.getTimestamp());
        
        X       = Float.parseFloat(data[ 3]);       // Velocity X
        Y       = Float.parseFloat(data[ 4]);       // Velocity Y
        Z       = Float.parseFloat(data[ 5]);       // Velocity Z
        velocity = new VelocityContainer(X, Y, Z, this.getTimestamp());
        
        X       = Float.parseFloat(data[ 6]);       // Heading X
        Y       = Float.parseFloat(data[ 7]);       // Heading Y
        Z       = Float.parseFloat(data[ 8]);       // Heading Z
        heading = new HeadingContainer(X, Y, Z, this.getTimestamp());
        
        X       = Float.parseFloat(data[ 9]);       // Acceleration X
        Y       = Float.parseFloat(data[10]);       // Acceleration Y
        Z       = Float.parseFloat(data[11]);       // Acceleration Z
        V       = Float.parseFloat(data[12]);       // Acceleration Vector 
        acceleration = new AccelerationContainer(X, Y, Z, V, this.getTimestamp());
        
        double X1       = Double.parseDouble(data[13]);       // Altitude
        double V1       = Double.parseDouble(data[14]);       // Altitude above Ground
        Y       = Float.parseFloat(data[15]);       // Latitude
        Z       = Float.parseFloat(data[16]);       // Longitude
        location = new LocationContainer(X1, Z, Y, V1, this.getTimestamp());
        
        X       = Float.parseFloat(data[20]);       // Dynamic Pressure
        Y       = Float.parseFloat(data[19]);       // Static Pressure
        Z       = Float.parseFloat(data[21]);       // Pressure Multiplier
        V       = Float.parseFloat(data[18]);       // Roh
        float U = Float.parseFloat(data[22]);       // Temperature
        enviro = new EnviroContainer(X, Y, Z, V, U, this.getTimestamp());
        
        uuid     = data[25];
        body   = data[17];
    }

    /**
     * 
     * @return
     */
    public float getUTC() {
        return UTC;
    }

    /**
     * 
     * @return
     */
    public AccelerationContainer getAcceleration() {
        return acceleration;
    }

    /**
     * 
     * @return
     */
    public String getBody() {
        return body;
    }

    /**
     * 
     * @return
     */
    public HeadingContainer getHeading() {
        return heading;
    }

    /**
     * 
     * @return
     */
    public LocationContainer getLocation() {
        return location;
    }

    /**
     * 
     * @return
     */
    public EnviroContainer getEnviro() {
        return enviro;
    }

    /**
     * 
     * @return
     */
    public RotationContainer getRotation() {
        return rotation;
    }

    /**
     * 
     * @return
     */
    public String getUuid() {
        return uuid;
    }

    /**
     * 
     * @return
     */
    public VelocityContainer getVelocity() {
        return velocity;
    }

    @Override
    public Float getTimestampDifference(Float timestamp) {
        return Math.abs(this.getTimestamp() - timestamp);
    }

    @Override
    public String toCSV() {
        return new Float(UTC).toString() 
                + ";" + new Float(this.getTimestamp()).toString() 
                + ";" + uuid 
                + ";" + body
                + ";" + velocity.toCSV()
                + ";" + acceleration.toCSV()
                + ";" + location.toCSV()
                + ";" + heading.toCSV()
                + ";" + rotation.toCSV()
                + ";" + enviro.toCSV();
    }

    @Override
    public String getHeader() {
        return "UTC;MET;UUID;Body" 
                + ";" + velocity.getHeader()
                + ";" + acceleration.getHeader()
                + ";" + location.getHeader()
                + ";" + heading.getHeader()
                + ";" + rotation.getHeader()
                + ";" + enviro.getHeader();
    }

    @Override
    public int compareTo(DataContainer o) {
        boolean vel = velocity.compareTo(o.getVelocity()) == 0;
        boolean acc = acceleration.compareTo(o.getAcceleration()) == 0;
        boolean loc = location.compareTo(o.getLocation()) == 0;
        boolean head = heading.compareTo(o.getHeading()) == 0;
        boolean rot = rotation.compareTo(o.getRotation()) == 0;
        boolean pres = enviro.compareTo(o.getEnviro()) == 0;
        boolean misc = UTC == o.getUTC() && this.getTimestamp() == o.getTimestamp()
                && body == o.getBody();
        if (vel && acc && loc && head && rot && pres && misc) {
            return 0;
        } else {
            return 1;
        }
    }
}
